/**
 * Copyright 2013-2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */	  

package com.cos.controller.app.appuser.role;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cos.controller.app.model.AppGroup;
import com.cos.controller.base.BaseController;
import com.cos.service.interfaces.role.IntMenuService;
import com.cos.service.interfaces.role.IntRoleService;
import com.cos.util.AppUtil;
import com.cos.util.PageData;
import com.cos.util.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 角色以及功能定制
 * <p/>
 * 
 * @author <a href="mailto:978954852@qq.com">xfjin</a>
 * @version  Date: 2016年4月1日 下午1:43:57
 * @serial 1.0
 * @since 2016年4月1日 下午1:43:57
 */
@Controller
@RequestMapping("appuser")
public class IntRoleController extends BaseController{
	
	@Resource 
	private IntRoleService intRoleService;
	@Resource
	private IntMenuService intMenuService;
	
	
	/**
	 * 通过用户ID获取功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getMenuByUID")
	@ResponseBody
	public String getMenuByUID(HttpServletResponse response) throws Exception{
		logBefore(logger, "根据用户ID获得用户群组");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getGroup", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				menuList = intMenuService.getMenuByUID(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
	
	/**
	 * 通过公司ID获取功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getMenuByCID")
	@ResponseBody
	public String getMenuByCID(HttpServletResponse response) throws Exception{
		logBefore(logger, "根据公司ID获得功能");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getDepartment", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				menuList = intMenuService.getMenuByCID(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
	
	
	/**
	 * 首页里的我的里面的更多 那个功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getMoreUserMenu")
	@ResponseBody
	public String getMoreUserMenu(HttpServletResponse response) throws Exception{
		logBefore(logger, "获得更多功能");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getGroup", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				menuList = intMenuService.getMoreUserMenu(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
	/**
	 * 我的里面更多功能里面去新增或删除功能
	 * @param response  U_ID MENU_ID STATUS
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="/saveUserMenu")
	@ResponseBody
	public String saveUserMenu(HttpServletResponse response) throws Exception{
		logBefore(logger, "获得更多功能");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("saveUserMenu", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				 intMenuService.updateUserMenu(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
	
	/**
	 * 首页里的公司里面的更多 那个功能
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getMoreCompanyMenu")
	@ResponseBody
	public String getMoreCompanyMenu(HttpServletResponse response) throws Exception{
		logBefore(logger, "获得更多功能");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("getDepartment", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				menuList = intMenuService.getMoreCompanyMenu(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
	
	/**
	 * 公司里面更多功能里面去新增或删除功能
	 * @param response  C_ID MENU_ID STATUS
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="/saveCompanyMenu")
	@ResponseBody
	public String saveCompanyMenu(HttpServletResponse response) throws Exception{
		logBefore(logger, "获得更多功能");
		PageData pd = new PageData();
		JSONObject group=new JSONObject();
		pd = this.getPageData();
		String result = "00";
		try{
			if(AppUtil.checkParam("saveCompanyMenu", pd)){	//检查参数
				List<PageData> menuList = new ArrayList<PageData>();
				 intMenuService.updateCompanyMenu(pd);
				JSONArray rows = JSONArray.fromObject(menuList);
				result = (null == pd) ?  "02" :  "01";
				group.put("rows", rows);
			}else {
				result = "03";
			}
		}catch (Exception e){
			logger.error(e.toString(), e);
		}finally{
			group.put("result", result);
			logAfter(logger);
		}
		ResponseUtil.write(response, group);
		return null;
	}
}
